#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "settings.h"
#include "eeprom_m24.h"
#include "main.h"
#include "computer_interface.h"

// Sends settings data to the computer
void settings_update_to_computer(uint8_t *data, uint16_t offset, uint8_t length) {
  //PRINTF("LENGTH: %i - OFFSET: %i\n",length,offset);

/*  for (uint8_t i=0;i<length;i++) {
    PRINTF("0x%02X ",data[i]);
  }
  PRINTF("\n\n\n");
*/
  if (computer_interface_connection_status())
    comm_interface_add_tx_struct_message(SRI_CMD_SEND_SETTINGS, length, offset, data);
}

void settings_update_from_computer(uint8_t *data, uint16_t offset, uint8_t length) {
  uint8_t *ptr = (uint8_t *)&settings;

  for (uint8_t i=0;i<length;i++)
    *(ptr+offset+i) = data[i];
}

void settings_save_to_eeprom(void) {
  eeprom_m24_write_block(SETTINGS_START_ADDRESS, sizeof(settings),(uint8_t *)&settings);
}

void settings_load_from_eeprom(void) {
  eeprom_m24_read_block(SETTINGS_START_ADDRESS, sizeof(settings),(uint8_t *)&settings);
}

/********** START SET RADIO SETTINGS **********/
void settings_set_radio_model(uint8_t value) {
  settings.radio.radio_model = value;

  SETTINGS_UPDATE_TO_COMPUTER(settings.radio.radio_model);
}

void settings_set_cat_interface_enabled(uint8_t value) {
  settings.radio.cat_interface_enabled = value;

  SETTINGS_UPDATE_TO_COMPUTER(settings.radio.cat_interface_enabled);
}

void settings_set_cat_baudrate(uint32_t value) {
  settings.radio.cat_baudrate = value;

  SETTINGS_UPDATE_TO_COMPUTER(settings.radio.cat_baudrate);
}

void settings_set_cat_stopbits(uint8_t value) {
  settings.radio.cat_stopbits = value;

  SETTINGS_UPDATE_TO_COMPUTER(settings.radio.cat_stopbits);
}

void settings_set_cat_parity(uint8_t value) {
  settings.radio.cat_parity = value;

  SETTINGS_UPDATE_TO_COMPUTER(settings.radio.cat_parity);
}

void settings_set_cat_icom_civ(uint8_t value) {
  settings.radio.cat_icom_civ = value;

  SETTINGS_UPDATE_TO_COMPUTER(settings.radio.cat_icom_civ);
}

void settings_set_cat_flow_control(uint8_t value) {
  settings.radio.cat_flow_control = value;

  SETTINGS_UPDATE_TO_COMPUTER(settings.radio.cat_flow_control);
}

void settings_set_cat_jumper_cts_rts(uint8_t value) {
  settings.radio.cat_jumper_rts_cts = value;

  SETTINGS_UPDATE_TO_COMPUTER(settings.radio.cat_jumper_rts_cts);
}

void settings_set_cat_retrieve_type(uint8_t value) {
  settings.radio.cat_retrieve_type = value;

  SETTINGS_UPDATE_TO_COMPUTER(settings.radio.cat_retrieve_type);
}

void settings_set_cat_poll_rate(uint8_t value) {
  settings.radio.cat_poll_rate = value;

  SETTINGS_UPDATE_TO_COMPUTER(settings.radio.cat_poll_rate);
}

/********** END SET RADIO SETTINGS **********/

/********** START GET RADIO SETTINGS **********/

uint8_t settings_get_radio_model(void) {
  return(settings.radio.radio_model);
}

uint8_t settings_get_cat_interface_enabled(void) {
  return(settings.radio.cat_interface_enabled);
}

uint32_t settings_get_cat_baudrate(void) {
  return(settings.radio.cat_baudrate);
}

uint8_t settings_get_cat_stopbits(void) {
  return(settings.radio.cat_stopbits);
}

uint8_t settings_get_cat_parity(void) {
  return(settings.radio.cat_parity);
}

uint8_t settings_get_cat_icom_civ(void) {
  return(settings.radio.cat_icom_civ);
}

uint8_t settings_get_cat_flow_control(void) {
  return(settings.radio.cat_flow_control);
}

uint8_t settings_get_cat_jumper_cts_rts(void) {
  return(settings.radio.cat_jumper_rts_cts);
}

uint8_t settings_get_cat_retrieve_type(void) {
  return(settings.radio.cat_retrieve_type);
}

uint8_t settings_get_cat_poll_rate(void) {
  return(settings.radio.cat_poll_rate);
}

/********** END GET RADIO SETTINGS **********/

/********** START GET PTT SETTINGS **********/
uint8_t settings_get_ptt_input_enabled(void) {
  return(settings.ptt.ptt_input_enabled);
}

uint8_t settings_get_ptt_input_invert(void) {
  return(settings.ptt.ptt_input_invert);
}

uint8_t settings_get_ptt_radio_pre_delay(void) {
  return(settings.ptt.radio_pre_delay);
}

uint8_t settings_get_ptt_radio_post_delay(void) {
  return(settings.ptt.radio_post_delay);
}

uint8_t settings_get_ptt_amp_pre_delay(void) {
  return(settings.ptt.amp_pre_delay);
}

uint8_t settings_get_ptt_amp_post_delay(void) {
  return(settings.ptt.amp_post_delay);
}

uint8_t settings_get_ptt_inhibit_pre_delay(void) {
  return(settings.ptt.inhibit_pre_delay);
}

uint8_t settings_get_ptt_inhibit_post_delay(void) {
  return(settings.ptt.inhibit_post_delay);
}

uint8_t settings_get_ptt_antenna_pre_delay(void) {
  return(settings.ptt.antenna_pre_delay);
}

uint8_t settings_get_ptt_antenna_post_delay(void) {
  return(settings.ptt.antenna_post_delay);
}

uint8_t settings_get_radio_digital_ptt(void) {
  return(settings.ptt.radio_digital_ptt);
}

uint8_t settings_get_radio_cw_ptt(void) {
  return(settings.ptt.radio_cw_ptt);
}

uint8_t settings_get_radio_phone_ptt(void) {
  return(settings.ptt.radio_phone_ptt);
}

uint8_t settings_get_amplifier_enabled(void) {
  return(settings.ptt.amplifier_enabled);
}

uint8_t settings_get_inhibit_enabled(void) {
  return(settings.ptt.inhibit_enabled);
}
/********** END GET PTT SETTINGS **********/

/********** START SET PTT SETTINGS **********/
void settings_set_ptt_input_enabled(uint8_t value) {
  settings.ptt.ptt_input_enabled = value;

  SETTINGS_UPDATE_TO_COMPUTER(settings.ptt.ptt_input_enabled);
}

void settings_set_ptt_input_invert(uint8_t value) {
  settings.ptt.ptt_input_invert = value;

  SETTINGS_UPDATE_TO_COMPUTER(settings.ptt.ptt_input_invert);
}

void settings_set_ptt_radio_pre_delay(uint8_t value) {
  settings.ptt.radio_pre_delay = value;

  SETTINGS_UPDATE_TO_COMPUTER(settings.ptt.radio_pre_delay);
}

void settings_set_ptt_radio_post_delay(uint8_t value) {
  settings.ptt.radio_post_delay = value;

  SETTINGS_UPDATE_TO_COMPUTER(settings.ptt.radio_post_delay);
}

void settings_set_ptt_amp_pre_delay(uint8_t value) {
  settings.ptt.amp_pre_delay = value;

  SETTINGS_UPDATE_TO_COMPUTER(settings.ptt.amp_pre_delay);
}

void settings_set_ptt_amp_post_delay(uint8_t value) {
  settings.ptt.amp_post_delay = value;

  SETTINGS_UPDATE_TO_COMPUTER(settings.ptt.amp_post_delay);
}

void settings_set_ptt_inhibit_pre_delay(uint8_t value) {
  settings.ptt.inhibit_pre_delay = value;

  SETTINGS_UPDATE_TO_COMPUTER(settings.ptt.inhibit_pre_delay);
}

void settings_set_ptt_inhibit_post_delay(uint8_t value) {
  settings.ptt.inhibit_post_delay = value;

  SETTINGS_UPDATE_TO_COMPUTER(settings.ptt.inhibit_post_delay);
}

void settings_set_ptt_antenna_pre_delay(uint8_t value) {
  settings.ptt.antenna_pre_delay = value;

  SETTINGS_UPDATE_TO_COMPUTER(settings.ptt.antenna_pre_delay);
}

void settings_set_ptt_antenna_post_delay(uint8_t value) {
  settings.ptt.antenna_post_delay = value;

  SETTINGS_UPDATE_TO_COMPUTER(settings.ptt.antenna_post_delay);
}

void settings_set_radio_digital_ptt(uint8_t value) {
  settings.ptt.radio_digital_ptt = value;

  SETTINGS_UPDATE_TO_COMPUTER(settings.ptt.radio_digital_ptt);
}

void settings_set_radio_cw_ptt(uint8_t value) {
  settings.ptt.radio_cw_ptt = value;

  SETTINGS_UPDATE_TO_COMPUTER(settings.ptt.radio_cw_ptt);
}

void settings_set_radio_phone_ptt(uint8_t value) {
  settings.ptt.radio_phone_ptt = value;

  SETTINGS_UPDATE_TO_COMPUTER(settings.ptt.radio_phone_ptt);
}

void settings_set_amplifier_enabled(uint8_t value) {
  settings.ptt.amplifier_enabled = value;

  SETTINGS_UPDATE_TO_COMPUTER(settings.ptt.amplifier_enabled);
}

void settings_set_inhibit_enabled(uint8_t value) {
  settings.ptt.inhibit_enabled = value;

  SETTINGS_UPDATE_TO_COMPUTER(settings.ptt.inhibit_enabled);
}
/********** END SET PTT SETTINGS **********/

/********** START GET AUDIO SETTINGS **********/
void settings_set_af_input_source_cw(uint8_t type, uint8_t state) {
  settings.audio.af_input_source_cw[type] = state;

  SETTINGS_UPDATE_TO_COMPUTER(settings.audio.af_input_source_cw);
}

void settings_set_af_input_source_phone(uint8_t type, uint8_t state) {
  settings.audio.af_input_source_phone[type] = state;

  SETTINGS_UPDATE_TO_COMPUTER(settings.audio.af_input_source_phone);
}

void settings_set_af_input_source_digital(uint8_t type, uint8_t state) {
  settings.audio.af_input_source_digital[type] = state;

  SETTINGS_UPDATE_TO_COMPUTER(settings.audio.af_input_source_digital);
}

void settings_set_mic_path(uint8_t type, uint8_t state) {
  settings.audio.mic_path[type] = state;

  SETTINGS_UPDATE_TO_COMPUTER(settings.audio.mic_path);
}

void settings_set_computer_generated_path_phone(uint8_t type, uint8_t state) {
  settings.audio.computer_generated_path_phone[type] = state;

  SETTINGS_UPDATE_TO_COMPUTER(settings.audio.computer_generated_path_phone);
}

void settings_set_computer_generated_path_digital(uint8_t type, uint8_t state) {
  settings.audio.computer_generated_path_digital[type] = state;

  SETTINGS_UPDATE_TO_COMPUTER(settings.audio.computer_generated_path_digital);
}

void settings_set_mic_mute(uint8_t type, uint8_t state) {
  settings.audio.mic_mute[type] = state;

  SETTINGS_UPDATE_TO_COMPUTER(settings.audio.mic_mute);
}

void settings_set_mic_preamp(uint8_t type, uint8_t state) {
  settings.audio.mic_preamp[type] = state;

  SETTINGS_UPDATE_TO_COMPUTER(settings.audio.mic_preamp);
}

void settings_set_line_out_mute_phone(uint8_t type, uint8_t state) {
  settings.audio.line_out_mute_phone[type] = state;

  SETTINGS_UPDATE_TO_COMPUTER(settings.audio.line_out_mute_phone);
}

void settings_set_line_out_mute_digital(uint8_t type, uint8_t state) {
  settings.audio.line_out_mute_digital[type] = state;

  SETTINGS_UPDATE_TO_COMPUTER(settings.audio.line_out_mute_digital);
}
/********** END GET AUDIO SETTINGS **********/

/********** START GET AUDIO SETTINGS **********/
uint8_t settings_get_af_input_source_cw(uint8_t type) {
  return(settings.audio.af_input_source_cw[type]);
}

uint8_t settings_get_af_input_source_phone(uint8_t type) {
  return(settings.audio.af_input_source_phone[type]);
}

uint8_t settings_get_af_input_source_digital(uint8_t type) {
  return(settings.audio.af_input_source_digital[type]);
}

uint8_t settings_get_mic_path(uint8_t type) {
  return(settings.audio.mic_path[type]);
}

uint8_t settings_get_computer_generated_path_phone(uint8_t type) {
  return(settings.audio.computer_generated_path_phone[type]);
}

uint8_t settings_get_computer_generated_path_digital(uint8_t type) {
  return(settings.audio.computer_generated_path_digital[type]);
}

uint8_t settings_get_mic_mute(uint8_t type) {
  return(settings.audio.mic_mute[type]);
}

uint8_t settings_get_mic_preamp(uint8_t type) {
  return(settings.audio.mic_preamp[type]);
}

uint8_t settings_get_line_out_mute_phone(uint8_t type) {
  return(settings.audio.line_out_mute_phone[type]);
}

uint8_t settings_get_line_out_mute_digital(uint8_t type) {
  return(settings.audio.line_out_mute_digital[type]);
}
/********** END GET AUDIO SETTINGS **********/
